package net.csdn.business.mirror;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.adapter.AdapterService;
import net.csdn.business.common.domain.PageDto;
import net.csdn.business.common.domain.common.ConditionQueryDto;
import net.csdn.business.common.domain.mirror.MyGithubDto;
import net.csdn.business.common.domain.mirror.ProjectMirrorDataQueryDto;
import net.csdn.business.common.feign.yunxiao.model.request.ImportProjectDto;
import net.csdn.business.mirror.entity.bean.GithubToken;
import net.csdn.business.mirror.entity.bean.ProjectMirrorData;
import net.csdn.business.mirror.enums.UserTokenEnum;
import net.csdn.business.mirror.service.GithubTokenService;
import net.csdn.business.mirror.service.ProjectMirrorDataService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kohsuke.github.GHRateLimit;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;

@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"spring.profiles.active=dev", "logging.file.path=/Users/yanlp/IdeaProjects/atomgit/openatom-mirror/logs"})
@Slf4j
public class GithubSyncServiceTest {

    @Autowired
    private GithubTokenService githubTokenService;

    @Autowired
    private ProjectMirrorDataService projectMirrorDataService;

    @Autowired
    private AdapterService adapterService;
    @Test
    public void getAllToken() {
        List<GithubToken> list = githubTokenService.getAllToken();
        log.info("valid result: {}", list);
    }

    @Test
    public void githubApi() {
        List<GithubToken> list = githubTokenService.getAllToken();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (GithubToken githubToken : list) {
            GitHub gitHub = null;
            try {
                gitHub = new GitHubBuilder().withOAuthToken(githubToken.getToken()).build();
            } catch (IOException e) {
                continue;
            }

            try {
                GHRateLimit ghRateLimit = gitHub.getRateLimit();
                log.info("rateLimit: {}", ghRateLimit);
                if (ghRateLimit.getCore().getRemaining() <= 100) {
                    continue;
                }
            } catch (IOException e) {
                continue;
            }

            try {
                GHRepository ghRepository = gitHub.getRepository("vuejs/vue");
                log.info("ghRepository: {}", ghRepository);
                log.info("success");
                break;
            } catch (IOException e) {
                continue;
            }

        }
    }

    @Test
    public void listMirrorData() {
        ProjectMirrorDataQueryDto dto = new ProjectMirrorDataQueryDto();
        dto.setPageNum(1);
        dto.setPageSize(10);
        dto.setOrder(new ConditionQueryDto.Order("next_sync_execution_at", false));
        dto.setNextMinId(0L);
        PageDto<ProjectMirrorData> res = projectMirrorDataService.searchData(dto);
        log.info("result: {}", res);
    }

    @Test
    public void importGitee() {

        String repoPath = "openeuler/yocto-meta-ros";
        String fullPath = "importorg/yocto-meta-ros";
        String name = fullPath.split("/")[1];
        UserTokenEnum.Type t = UserTokenEnum.Type.GITEE;
        MyGithubDto.Repository repository = SpringBeanUtil.getBean(t.getThirdApiService()).userRepo("", repoPath);
        String desc = repository.getDescription();
        log.info(desc);


        ImportProjectDto dto = new ImportProjectDto();
        dto.setName(name);
        dto.setPath(name);
        dto.setVisibility_level(20L);
        dto.setDescription(desc);
        dto.setImport_url(t.getBaseUrl() + repoPath + ".git");
        dto.setSync(false);
        dto.setCreate_parent_path(false);
        dto.setExtern_uid("641adaf7d1b6adc7415562ee");
        dto.setOrganization_id("6470528003d7c179f4201559");
        dto.setTarget_org_id("6470528003d7c179f4201559");
        dto.setImport_account("yanlinpu");
        adapterService.importProject(dto);
        log.info("rrr");

    }


}